package gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

public class ColorEditor extends AbstractCellEditor implements TableCellEditor,
		ActionListener {
	private Color current;
	private JButton button;
	private JColorChooser chooser;
	private JDialog chooserView;
	
	public ColorEditor()
	{
		button=new JButton();
		button.setActionCommand("edit");
		button.addActionListener(this);
		button.setBorderPainted(false);
		chooser=new JColorChooser();
		chooserView=JColorChooser.createDialog(button, "Select Color", true, 
				chooser, this, null);
	}
	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int column) {
		current=(Color)value;
		return button;
	}

	public Object getCellEditorValue() {
		return current;
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("edit"))
		{
			button.setBackground(current);
			chooser.setColor(current);
			chooserView.setVisible(true);
			fireEditingStopped();
		}
		else
		{
			current=chooser.getColor();
		}
	}

}
